﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.IDAL;
using System.Data.SqlClient;

namespace Olympic_email_to_future.DAL
{
    class UserDAL : IUserInfo
    {
        UserInfoDataContext ctx = new UserInfoDataContext("Data Source=WIN-0HVEJRO90DS\\SQLEXPRESS;Initial Catalog=Olympic;Integrated Security=True");

        public UserInfo queryByEmail(string email)
        {
            UserInfo user = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == email);
            return user;
        }

        // 返回所有用户 ，用户后台管理
        public IList<UserInfo> getUsers()
        {
            IList<UserInfo> users = new List<UserInfo>();
            var result = from p in ctx.UserInfo select p;
            foreach (UserInfo p in result)
            {
                UserInfo user = new UserInfo();
                user.userId = p.userId;
                user.userPassword = p.userPassword;
                user.userMailAddress = p.userMailAddress;
                user.userRole = (int)p.userRole;
                users.Add(user);
            }
            if (users != null)
                return users;
            else
                return null;
        }

        public bool register(UserInfo userinfo)
        {
            if (queryByEmail(userinfo.userMailAddress) == null)
            {
                UserInfo user = new UserInfo();
                user.userMailAddress = userinfo.userMailAddress;
                user.userPassword = userinfo.userPassword;
                user.userRole = 0;
                user.userState = 0;
                ctx.UserInfo.InsertOnSubmit(user);
                ctx.SubmitChanges();
                return true;
            }
            else return false;
        }

        public bool login(UserInfo userinfo)
        {
            UserInfo user = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == userinfo.userMailAddress && c.userPassword == userinfo.userPassword);
            if (user != null)
                return true;
            else
                return false;
        }

        public UserInfo getUser(UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == user.userMailAddress);
            return userinfo;
        }

        public void updateUser(UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == user.userMailAddress);
            userinfo.userAddress = user.userAddress;
            userinfo.userPhone = user.userPhone;
            userinfo.userName = user.userName;
            userinfo.userSex = user.userSex;
            ctx.SubmitChanges();
        }
        //后台
        public void update(int id, UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userId == id);
            userinfo.userRole = user.userRole;
            ctx.SubmitChanges();
        }
        //后台
        public void delete(int id, UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userId == id);
            ctx.UserInfo.DeleteOnSubmit(userinfo);
            ctx.SubmitChanges();
        }

        public void modify_password(UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == user.userMailAddress);
            userinfo.userPassword = user.userPassword;
            ctx.SubmitChanges();
        }

        public bool checkPassword(UserInfo user)
        {
            UserInfo userinfo = ctx.UserInfo.SingleOrDefault(c => c.userMailAddress == user.userMailAddress && c.userPassword == user.userPassword);
            if (userinfo == null)
                return false;
            else
                return true;
        }

        public bool add(UserInfo userinfo)
        {
            if (queryByEmail(userinfo.userMailAddress) == null)
            {
                UserInfo user = new UserInfo();
                user.userMailAddress = userinfo.userMailAddress;
                user.userPassword = userinfo.userPassword;
                user.userRole = userinfo.userRole;
                user.userState = 0;
                ctx.UserInfo.InsertOnSubmit(user);
                ctx.SubmitChanges();
                return true;
            }
            else return false;
        }
    }
}